/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-5-25下午09:08:46
 */
package hit.edance.campusmobile.chat.adapter;

import hit.edance.campusmobile.chat.R;
import hit.edance.campusmobile.chat.activity.ActivityLogin;
import hit.edance.campusmobile.chat.model.FriendsManager;
import hit.edance.campusmobile.chat.model.User;
import hit.edance.campusmobile.chat.util.ConstantUtil;
import hit.edance.campusmobile.chat.util.PictureUtil;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterGroup;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 好友列表的adapter
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.adapter
 * @Class FriendsExpandableListAdapter
 * @Date 2012-5-25 下午09:08:46
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class FriendsExpandableListAdapter extends BaseExpandableListAdapter {

	private Roster roster;
	private Context context;
	FriendsManager friendsmanager;

	/*
	 * @see android.widget.ExpandableListAdapter#getGroupCount()getGroupCount
	 */

	public FriendsExpandableListAdapter(Context context, Roster roster) {

		this.roster = roster;
		this.context = context;
		init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		friendsmanager = FriendsManager.getInstance();
		friendsmanager.setGroupChildMap(roster);
		friendsmanager.sortGroupChildMap();
	}

	public int getGroupCount() {
		return roster.getGroupCount();
	}

	/*
	 * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
	 * getChildrenCount
	 */
	public int getChildrenCount(int groupPosition) {
		return friendsmanager.getGroupChildMap()
				.get(friendsmanager.getGroups().get(groupPosition).getName())
				.size();
	}

	/*
	 * @see android.widget.ExpandableListAdapter#getGroup(int)getGroup
	 */
	public RosterGroup getGroup(int groupPosition) {
		return friendsmanager.getGroups().get(groupPosition);
	}

	/*
	 * @see android.widget.ExpandableListAdapter#getChild(int, int)getChild
	 */
	public User getChild(int groupPosition, int childPosition) {
		if (friendsmanager.getGroupChildMap().get(friendsmanager.getGroups().get(groupPosition).getName())
				.size() >= childPosition)
			return friendsmanager.getGroupChildMap().get(
					friendsmanager.getGroups().get(groupPosition).getName()).get(
					childPosition);
		else
			return null;
	}

	/*
	 * @see android.widget.ExpandableListAdapter#getGroupId(int)getGroupId
	 */
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	/*
	 * @see android.widget.ExpandableListAdapter#getChildId(int, int)getChildId
	 */
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	/*
	 * @see android.widget.ExpandableListAdapter#hasStableIds()hasStableIds
	 */
	public boolean hasStableIds() {
		return false;
	}

	/*
	 * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean,
	 * android.view.View, android.view.ViewGroup)getGroupView
	 */
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupHolder holder;
		if (convertView == null) {
			holder = new GroupHolder();
			holder.tv = createGroupView();
			holder.tv.setPadding((int) (40 * ConstantUtil.density), 0, 0, 0);
			holder.tv.setTextSize(20*ConstantUtil.density);
			holder.tv.setTextColor(R.color.chat_orange);
			convertView = holder.tv;
			convertView.setTag(holder);
		} else {
			holder = (GroupHolder) convertView.getTag();
		}
		if(friendsmanager.getGroups().size() > 0)
		holder.tv.setText(friendsmanager.getGroups().get(groupPosition)
				.getName()
				+ "\t"
				+ "["
				+ friendsmanager.getOnLineCountInList(friendsmanager
						.getGroupChildMap().get(
								friendsmanager.getGroups().get(groupPosition)
										.getName()))
				+ "/"
				+ friendsmanager
						.getGroupChildMap()
						.get(friendsmanager.getGroups().get(groupPosition)
								.getName()).size() + "]");
		return convertView;
	}

	public TextView createGroupView() {
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				(int) (48 * ConstantUtil.density));
		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		textView.setPadding((int) (32 * ConstantUtil.density), 0, 0, 0);
		return textView;

	}

	/*
	 * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean,
	 * android.view.View, android.view.ViewGroup)getChildView
	 */
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildHolder holder;
		if (convertView == null) {
			holder = new ChildHolder();
			View v = View.inflate(context, R.layout.chat_friends_child_item, null);
			holder.head = (ImageView) v.findViewById(R.id.head);
			holder.nickname = (TextView) v.findViewById(R.id.nickname);
			holder.signature = (TextView) v.findViewById(R.id.signature);
			holder.status = (ImageView) v.findViewById(R.id.status);
			convertView = v;
			convertView.setTag(holder);
		} else {
			holder = (ChildHolder) convertView.getTag();
		}
		User user = null;
		if (friendsmanager.getGroupChildMap().get(friendsmanager.getGroups().get(groupPosition).getName())
				.size() >= childPosition)
			user = friendsmanager.getGroupChildMap().get(
					friendsmanager.getGroups().get(groupPosition).getName()).get(
					childPosition);
		Bitmap bmp = user.getIcon() == null ? PictureUtil
				.drawableToBitmap(context.getResources().getDrawable(
						R.drawable.chat_ic_contact_picture)) : user.getIcon();
		if (user.getStatus() != null) {
			holder.head.setImageBitmap(bmp);
			holder.status.setImageDrawable(context.getResources()
					.getDrawable(
							ActivityLogin.statusImg[Integer.parseInt(user
									.getStatus())]));
			holder.status.setVisibility(View.VISIBLE);

		} else {
			holder.head.setImageBitmap(PictureUtil.getRoundedCornerBitmap(
					PictureUtil.getGrayBitmap(bmp),
					PictureUtil.ROUNDPROPORTION_DEFAULT));
			holder.status.setVisibility(View.GONE);
		}

		holder.nickname.setText(user.getNickname() == null ? user.getJabberId()
				: user.getNickname());

		holder.signature.setText(user.getSignature() == null ? "此人很懒什么都没有留下"
				: user.getSignature());
		return convertView;

		// return createChildView(groupPosition,childPosition,
		// isLastChild,convertView,parent);

	}

	// private View createChildView(int groupPosition, int childPosition,
	// boolean isLastChild, View convertView, ViewGroup parent){
	//
	// ChildHolder holder = null;
	//
	// if(convertView == null){
	// holder = new ChildHolder();
	// RelativeLayout rl = new RelativeLayout(context); //创建相对布局
	// rl.setGravity(Gravity.CENTER_VERTICAL);
	// RelativeLayout.LayoutParams
	// params1 = new RelativeLayout.LayoutParams((int)(50*ConstantUtil.density),
	// (int)(50*ConstantUtil.density)),
	// params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
	// LayoutParams.WRAP_CONTENT),
	// params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
	// LayoutParams.WRAP_CONTENT);
	// params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
	// params1.addRule(RelativeLayout.CENTER_VERTICAL);
	// params2.addRule(RelativeLayout.RIGHT_OF,111);
	// params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
	// params3.addRule(RelativeLayout.CENTER_VERTICAL);
	// holder.head = new ImageView(context);
	// holder.head.setId(111);
	// holder.head.setScaleType(ImageView.ScaleType.FIT_XY);
	// holder.head.setPadding((int)(5*ConstantUtil.density), 0, 0, 0);
	// LinearLayout ll = new LinearLayout(context); //创建子线性布局
	// ll.setId(222);
	// ll.setOrientation(LinearLayout.VERTICAL);
	// holder.nickname = new TextView(context);
	// holder.nickname.setTextAppearance(context, R.style.text_2);
	// holder.nickname.setPadding((int)(5*ConstantUtil.density), 0, 0, 0);
	// holder.signature = new TextView(context); //创建用于显示心情的TextView
	// holder.signature.setTextAppearance(context, R.style.text_3);
	// holder.signature.setPadding((int)(5*ConstantUtil.density), 0, 0, 0);
	//
	// ImageView im = new ImageView(context); //创建ImageView对象
	// im.setScaleType(ImageView.ScaleType.FIT_XY);
	// im.setImageDrawable(context.getResources().getDrawable(R.drawable.expander_ic_minimized));
	// //设置右边小箭头
	// im.setPadding(0, 0, (int)(5*ConstantUtil.density), 0);
	//
	// ll.addView(holder.nickname); //将显示姓名的TextView添加到线性布局
	// ll.addView(holder.signature); //将显示心情的TextView添加到线性布局
	//
	// rl.addView(holder.head,params1); //将显示头像的ImageView添加到现象布局
	// rl.addView(ll,params2);
	// rl.addView(im,params3);
	// convertView = rl;
	// convertView.setTag(holder);
	// }else {
	// holder = (ChildHolder) convertView.getTag();
	// }
	// User user = null;
	// if(showmap.get(ActivityFriends.groups.get(groupPosition).getName()).size()
	// >= childPosition)
	// user =
	// showmap.get(ActivityFriends.groups.get(groupPosition).getName()).get(childPosition);
	// if(user.getStatus() != null){
	// holder.head.setImageBitmap(user.getIcon());
	//
	// }else{
	// holder.head.setImageBitmap(PictureUtil.getRoundedCornerBitmap(PictureUtil.getGrayBitmap(user.getIcon()),PictureUtil.ROUNDPROPORTION_DEFAULT));
	// }
	//
	// holder.nickname.setText(user.getNickname());
	//
	// holder.signature.setText("心情:"+user.getSignature());
	//
	// return convertView;
	// }

	/*
	 * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
	 * isChildSelectable
	 */
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	private class GroupHolder {
		TextView tv;
	}

	private class ChildHolder {
		ImageView head;
		ImageView status;
		TextView nickname;
		TextView signature;
	}
}
